﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Data.Entity;
using System.Web;
using System.Web.Mvc;

namespace HdipCricketClub.Models
{
    public class ClubOtherFixtureDetailRepository
    {
        CricketClubEntities db = new CricketClubEntities();

        public ClubOtherFixtureDetail GetClubOtherFixtureDetail(Fixture fixture)
        {
            ClubOtherFixtureDetail clubOtherFixtureDetail = db.ClubOtherFixtureDetails.Where(cofd => cofd.fixture_id == fixture.fixture_id).Single();

            return clubOtherFixtureDetail;
        }

        public ClubOtherFixtureDetail GetClubOtherFixtureDetail(int id)
        {
            ClubOtherFixtureDetail clubOtherFixtureDetail = db.ClubOtherFixtureDetails.Where(cofd => cofd.fixture_id == id).Single();

            return clubOtherFixtureDetail;
        }
        
        public double GetTotalClubExtras(ClubOtherFixtureDetail clubOtherFixtureDetail)
        {
            return (double)(clubOtherFixtureDetail.byes + clubOtherFixtureDetail.leg_byes + clubOtherFixtureDetail.wides + clubOtherFixtureDetail.no_balls);
        }

        public void ParseFormCollection(Fixture fixture, FormCollection formCollection)
        {
            string[] fcByes = formCollection.GetValues("ClubOtherDetail.byes");
            string[] fcLegByes = formCollection.GetValues("ClubOtherDetail.leg_byes");
            string[] fcWides = formCollection.GetValues("ClubOtherDetail.wides");
            string[] fcNoBalls = formCollection.GetValues("ClubOtherDetail.no_balls");

            ClubOtherFixtureDetail clubOtherFixtureDetail = new ClubOtherFixtureDetail()
            {
                fixture_id = fixture.fixture_id
            };

            if (fcByes[0] != "")
            {
                clubOtherFixtureDetail.byes = double.Parse(fcByes[0]);
            }

            if (fcLegByes[0] != "")
            {
                clubOtherFixtureDetail.leg_byes = double.Parse(fcLegByes[0]);
            }

            if (fcWides[0] != "")
            {
                clubOtherFixtureDetail.wides = double.Parse(fcWides[0]);
            }

            if (fcNoBalls[0] != "")
            {
                clubOtherFixtureDetail.no_balls = double.Parse(fcNoBalls[0]);
            }

            db.ClubOtherFixtureDetails.AddObject(clubOtherFixtureDetail);
            db.SaveChanges();

        }


    }
}